package com.hdoit.apollo.web.cargo.apis;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Category;
import com.hdoit.apollo.logical.cargo.service.ICategoryService;
import com.hdoit.apollo.logical.viewmodel.cargo.CategorySearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;


@Controller
@RequestMapping(value = "/api/v1/category/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/category")
public class CategoryApi extends BaseApi{
	
	@Autowired
	private ICategoryService categoryService;
	
	private final String getCategoryInfoApi="getCategoryInfo";
	
	private final String saveCategoryApi="saveCategory";
	
	private final String deleteCategoryApi="deleteCategory";
	
	@RequestMapping(value = deleteCategoryApi)
    @ApiOperation(notes = deleteCategoryApi, httpMethod = "POST", value = "删除分类信息")
    public @ResponseBody String deleteCategory(String categoryId){
    	ResponseModel rm=new ResponseModel();
   
    	if(!categoryService.deleteCategory(categoryId)) {
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }
	
    @RequestMapping(value = getCategoryInfoApi)
    @ApiOperation(notes = getCategoryInfoApi, httpMethod = "GET", value = "货品分类列表")
    public @ResponseBody String getCategoryInfo(){
    	CategorySearch search=new CategorySearch();
    	List<Category> categories = categoryService.queryListMP(search,null);
    	
    	ResponseModel rm=new ResponseModel();
    	rm.put("categories",categories);
    	return rm.toJson();
    }
    
    
    @RequestMapping(value = saveCategoryApi)
    @ApiOperation(notes = saveCategoryApi, httpMethod = "POST", value = "货品分类信息")
    public @ResponseBody String saveCategory(Category category){
    	Category categoryed=categoryService.queryModel(category.getCategoryId());
    	if (categoryed==null) {
    		category.setCreateDate(DateUtils.getCurrent());
    		category.setCreateUserId(getLoginId());
    		category.setCreateUserName(getLoginName());
    		category.setDeleteMark(YesOrNoInteger.no);
    		category.setModelStatus(ModelStatus.add);
		}else {
			category.setModifyDate(DateUtils.getCurrent());
			category.setModifyUserId(getLoginId());
			category.setModifyUserName(getLoginName());
			category.setModelStatus(ModelStatus.update);
			category.setDeleteMark(categoryed.getDeleteMark());
		}
    	
     	ResponseModel rm=new ResponseModel();
     	if(!categoryService.applyChange(category)){
     		rm.setStatus(ResponseStatus.dataError);
     	}
     	
     	return rm.toJson();
    }
    

}

